<template>
    <el-row :gutter="20">
        <el-col :span="8">
            <el-button type="primary" :icon="CirclePlus" @click="onEdit(0)">添加</el-button>
        </el-col>
    </el-row>
    <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
        <el-table-column type="index" :index="indexMethod" label="序号" width="60" align="center" />
        <el-table-column prop="title" label="申请事项" show-overflow-tooltip align="center" />
        <el-table-column prop="content" label="具体内容" show-overflow-tooltip align="center" />
        <el-table-column label="建议人" show-overflow-tooltip align="center">
            <template #default="scope">
                <el-text>{{scope.row.user.name}}</el-text>
            </template>
        </el-table-column>
       
        <el-table-column prop="reply" label="回复" show-overflow-tooltip align="center" />
        <el-table-column prop="created_at" label="日期" show-overflow-tooltip align="center" />
        <el-table-column fixed="right" label="操作" width="120" align="center">
            <template #default="scope">
                <el-link type="primary" @click="onEdit(scope.row.id)" >查看</el-link>
                <el-link  type="primary" v-loading="applyLoading" @click="onApply(scope.row)">审批</el-link>
            </template>
        </el-table-column>
    </el-table>
    <div class="mt20">
        <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  
    <el-dialog v-model="isEdit" title="" width="900px">
        <component :is="otherForm" :data = editId :type="false" @onSave="onSave" />
    </el-dialog>

    <el-dialog v-model="isApplyRecord" title="申请进度" width="50%">
        <component :is="applyRecord" :id = editId />
    </el-dialog>

</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import { CirclePlus} from '@element-plus/icons-vue'
import otherForm from '@/components/otherForm.vue';
import applyRecord from "@/components/applyRecord.vue";
import http from '../../../util/request';
import { ElMessage } from 'element-plus';
const indexMethod = (index: number) => {
  return index+= 1
}
const form = ref<any>({search:'',model:'Other',page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
const isEdit = ref<boolean>(false)
const isApplyRecord = ref(false)
const editId = ref<number>(0)
const tableData = ref<any[]>([])
const isApply = ref(false)
const applyLoading = ref<boolean>(false)

const onGetData = () => {
    
    form.value.loading = true
    tableData.value = []
    http.post('/other',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}

const onEdit = (id: number) => {
    editId.value = id
    isEdit.value = true
}

const onApply = (data: any) => {
    // editId.value = id
    // isApply.value = true
    applyLoading.value = true
    http.put('/approve',{source_id:data.id,project_id:data.project_id,reason:data.purpose,model:'Other',source:'Other',type:'apply'}).then((response:any)=>{
        applyLoading.value = false
        if(response.data.code != 200){
            ElMessage.error(response.data.msg);return
        }
        ElMessage.success(response.data.msg);
        onApplySave()  
    })
}

const onApplySave = () => {
    isApply.value = false
    onGetData()
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
}
onMounted(()=>{
    onGetData()
})

const onSave = () => {
    isEdit.value = false
    onGetData()
}

</script>

<style scoped>

</style>